﻿const router = require('koa-router')()
const withdrawlModel = require("../models/withdrawal")
const keyaccountModel = require("../models/keyaccount")
const orderModel = require("../models/order")
const partnerModel = require("../models/partner")
//引入时间取件处理函数
const screenArry = require("../config/screenArray")
router.prefix("/api");


//提现获取申请表
router.post("/getWithdrawStatus", async (ctx) => {
    ctx.status = 200
    let data = ctx.request.body
    let { name, status, startTime, endTime, operate, _id, all } = data
    let withdrawlRes = await withdrawlModel.find({})
    let tarArr = []
    if (name) {
        withdrawlRes.forEach((item, idx) => {
            if (item.name != name) {
                withdrawlRes.splice(idx, 1, 1)
            }
        })
    }
    if (status) {
        withdrawlRes.forEach((item, idx) => {
            if (!item.cashStatus != status) {
                withdrawlRes.splice(idx, 1, 1)
            }
        })
    }
    withdrawlRes.forEach((item) => {
        if (item != 1) {
            tarArr.push(item)
        }
    })
    if (operate) {
        if (operate == 1) {
            let getCash = withdrawlRes.getCash
            let res = await withdrawlModel.updateOne({ _id }, { cashStatus: "提现中", failed: "" })
            ctx.body = {
                code: 200,
                msg: "操作成功",
            }
        } else if (operate == 0) {
            let res = await withdrawlModel.updateOne({ _id }, { failed: "提现审核不被通过" })
            ctx.body = {
                code: 200,
                msg: "操作成功",
            }
        } else if (operate == 2) {
            let res
            let resAll = await withdrawlModel.find({})
            for (let i in resAll) {
                let temp = resAll[i]._id
                res = await withdrawlModel.updateOne({ _id: temp }, { cashStatus: "提现中", failed: "" })
            }

            ctx.body = {
                code: 200,
                msg: "操作成功",
            }
        }
    }
    if (endTime) {
        console.log(222)
        withdrawlRes = screenArry(withdrawlRes, "getDate", { startTime, endTime })
        tarArr = withdrawlRes
    }
    if (!operate) {
        ctx.body = {
            code: 200,
            tarArr
        }
    }
})


//根据日期查询
router.post("/getDateInOut", async (ctx) => {
    ctx.status = 200
    let { _id, startTime, endTime, type } = ctx.request.body
    let res = []
    let Wallet_details = (await partnerModel.findOne({ _id })).Wallet_details
    if (!Wallet_details) {
        Wallet_details = (await keyaccountModel.findOne({ _id })).Wallet_details
    }
    if (Wallet_details && endTime) {
        res = screenArry(Wallet_details, "inOutDate", { startTime, endTime })
    }
    if (type == 1) {
        res.forEach((item, idx) => {
            if (item.inOrOut == 0) {
                res.splice(idx, 1)
            }
        })
    }
    if (type == 0) {
        res.forEach((item, idx) => {
            if (item.inOrOut == 1) {
                res.splice(idx, 1)
            }
        })
    }
    ctx.body = {
        code: 200,
        res: res

    }
})
//按照姓名查询合伙人
router.post("/getParNameInfo", async (ctx) => {
    ctx.status = 200
    let { name } = ctx.request.body;
    let keyRes = await partnerModel.find({})
    let orderRes = await orderModel.find({})
    let withdrawlRes = await withdrawlModel.find({})
    let nameArr = []
    keyRes.forEach((item) => {
        if (item.partner_name.includes(name)) {
            nameArr.push(item.partner_name);
        }
    })

    let soldCount = getsold(keyRes, orderRes, withdrawlRes, nameArr)
    ctx.body = {
        code: 200,
        msg: soldCount
    }
})

//按照姓名查询大客户

router.post("/getKeyNameInfo", async (ctx) => {
    ctx.status = 200
    let { name } = ctx.request.body;
    let keyRes = await keyaccountModel.find({})
    let orderRes = await orderModel.find({})
    let withdrawlRes = await withdrawlModel.find({})
    let nameArr = []
    keyRes.forEach((item) => {
        if (item.Sales_name.includes(name)) {
            nameArr.push(item.Sales_name);
        }
    })

    let soldCount = getsold(keyRes, orderRes, withdrawlRes, nameArr)
    ctx.body = {
        code: 200,
        msg: soldCount
    }
})
//获取合伙人钱包相关数据
router.get("/getPartnerSum", async (ctx) => {
    ctx.status = 200;
    let keyRes = await partnerModel.find({})
    let orderRes = await orderModel.find({})
    let withdrawlRes = await withdrawlModel.find({})
    let nameArr = []
    keyRes.forEach((item) => {
        if (item.partner_name in nameArr) {
            return;
        } else {
            nameArr.push(item.partner_name);
        }
    })
    let soldCount = getsold(keyRes, orderRes, withdrawlRes, nameArr)
    ctx.body = {
        code: 200,
        msg: soldCount
    }
})
//钱包详细页面
router.post("/getWalletInfo", async (ctx) => {
    ctx.status = 200
    let { name } = ctx.request.body
    let res = await partnerModel.findOne({ partner_name: name })
    let raa = await keyaccountModel.findOne({ Sales_name: name })

 if (res != null) {
        console.log(1111);
        ctx.body = {
            code: 200,
            data: res
        }
    } else {
        console.log(2222);
        ctx.body = {
            code: 200,
            data: raa
        }
    }
})


//获取代理相关数据
router.get("/getAgentSum", async (ctx) => {
    ctx.status = 200
    let keyRes = await keyaccountModel.find({})
    let orderRes = await orderModel.find({})
    let withdrawlRes = await withdrawlModel.find({})
    //获取用户姓名数组
    let nameArr = []
    //姓名和订单金额新的数据结构
    keyRes.forEach((item) => {
        if (item.Sales_name in nameArr) {
            return
        }
        else {
            nameArr.push(item.Sales_name)
        }
    })
    let soldCount = getsold(keyRes, orderRes, withdrawlRes, nameArr)
    ctx.body = {
        code: 200,
        res: soldCount
    }
})
//添加提现列表数据
router.post("/setWithdrawal", async (ctx) => {
    let data = ctx.request.body
    let getDate = Date.now()
    let { name, idcard, phone, getCash, cash, bankCard, cashStatus, failed } = data
    let res = await withdrawlModel.create({ name, idcard, phone, getCash, cash, bankCard, getDate, cashStatus, failed })
    if (res) {
        ctx.body = {
            code: 200,
            msg: "提现列表数据成功",
        }
    } else {
        ctx.body = {
            code: 400,
            msg: "提现列表数据失败",
        }
    }
})

//粗略封装的多表联合查询的函数
var getsold = function (keyRes, orderRes, withdrawlRes, nameArr) {
    let soldCount = [];
    nameArr.forEach((item) => {
        soldCount.push({ name: item, soldBounus: 0, cash: 0 })
    })
    soldCount.forEach((item) => {
        for (let i in orderRes) {
            if (item.name == orderRes[i].name) {
                if (!item.province) {
                    item.province = orderRes[i].province
                    item.manaBounus = 1000;
                }
                if (orderRes[i].card_type == "年卡") {
                    item.soldBounus += orderRes[i].order_quantity * 80
                } else {
                    item.soldBounus += orderRes[i].order_quantity * 200
                }
            }
        }
        for (let j in withdrawlRes) {
            if (item.name == withdrawlRes[j].name) {
                item.cash += withdrawlRes[j].cash
                item.phone = withdrawlRes[j].phone
            }
        }
        item.allcount = item.soldBounus + item.cash + item.manaBounus
        let tempName = item.name
        keyRes.forEach((items) => {
            if (items.Sales_name == tempName) {
                item._id = items._id
            }
        })
    })
    return soldCount
}

module.exports = router
